ParticleSystem ps;
int timeCount = 5;

boolean clearBackground   = true;
boolean backgroundColored = false;

void setup()
{
  size(900, 700, P2D);
  //setting = new Settings();
  ps = new ParticleSystem(0, new PVector(0, 0));
}

void draw()
{
  if(clearBackground) background(255);

  ps.run();

  //espiral
  //ps.rotation = frameCount % width;
  //ps.rotation = (frameCount % width)*10; //com fator fica mais estreito
  
  color c = color(random(255), random(255), random(255));
  for (int i = 360; i > 0; i--)
  {
    //addParticle(i,c);
  }
  if (frameCount % timeCount == 0) {
    //addRandomParticle();

    addLinearParticle(Settings.LEFT, c);
    addLinearParticle(Settings.RIGHT, c);
    addLinearParticle(Settings.UP, c);
    addLinearParticle(Settings.DOWN, c);
    addLinearParticle(Settings.UP_LEFT, c);
    addLinearParticle(Settings.UP_RIGHT, c);
    addLinearParticle(Settings.DOWN_LEFT, c);
    addLinearParticle(Settings.DOWN_RIGHT, c);
  }

  if(backgroundColored) doPsyBackground();


  smooth();
}

void addParticle(int i, color c)
{
  Particle p;
  PVector accel = new PVector(cos(radians(i))*2, sin(radians(i))*2);
  p = new Particle(ps.origin, accel, new PVector(0, 0), ps.rotation, false, c);
  ps.addParticle(p);
}


void addLinearParticle(int dir, color c)
{
  Particle p;
  PVector accel = new PVector(0, 0);
  float f = 0.05;
  switch(dir)
  {
  case Settings.LEFT:
    accel.x = -f;
    break;
  case Settings.RIGHT:
    accel.x = f;
    break;
  case Settings.UP:
    accel.y = -f;
    break;
  case Settings.DOWN: 
    accel.y = f; 
    break;

  case Settings.UP_LEFT:
    accel.y = -f;
    accel.x = -f;
    break;
  case Settings.UP_RIGHT:
    accel.y = -f;
    accel.x = f;
    break;
  case Settings.DOWN_LEFT:
    accel.y = f;
    accel.x = -f;
    break;
  case Settings.DOWN_RIGHT:
    accel.y = f;
    accel.x = f;
    break;
  }

  p = new Particle(ps.origin, accel, new PVector(0, 0), ps.rotation, false, c );
  ps.addParticle(p);
}


void addRandomParticle()
{
  ps.addParticle();
}



/* CONTROLS
 *******************************/

void keyPressed()
{
  if (key == CODED) {
    if (keyCode == UP) {
      timeCount++;
    } 
    else if (keyCode == DOWN) {
      timeCount-= (timeCount > 1) ? 1 : 0;
    }
  }
}

void mouseMoved()
{
  ps.rotation = (mouseX / (float) width);
  //ps.rotation =   mouseX /width ;
}

/* DO
 *******************************/
void doPsyBackground() {
  pushMatrix();
  translate(-width/2, -height/2);
  noStroke();
  fill( abs(sin(radians(frameCount)) * 255), abs(sin(radians(frameCount*.7)) * 255), abs(sin(radians(frameCount*1.5)) * 255), 10);
  rect(0, 0, width, height);
  popMatrix();
}

